package com.sxm.yiwei.activitis;

import android.Manifest;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AlertDialog;
import android.support.v7.view.ContextThemeWrapper;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.tools.BaseIntents;
import com.sxm.yiwei.tools.CropPictures;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FWOpenShopActivity extends BaseActivity implements View.OnClickListener{

    private static final int FROM = 2;
    private Button possionshop;
    private Button teamshop;
    private ImageView openshopback;
    private LinearLayout teampic;
    private List<String> showimglist = new ArrayList();// 店铺展示
    private List<String> avatarimglist = new ArrayList();// 店铺头像
    private List<String> qualificationimglist = new ArrayList();// 店铺资质
    private File mCurrentPhotoFile;
    private static final int PHOTO_TEXT = 3022;
    private static final int PHOTO_PICKED_WITH_DATA = 3021;
    /*用来标识请求照相功能的activity*/
    private static final int CAMERA_WITH_DATA = 3023;
    private static final File PHOTO_DIR = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera");
    private Bitmap photo;
    private String path1;
    private  int currentIndex =0;
    private int imgtype = 0;
    private ImageView openimg,openimg1,openimg2,openimg3,openimg4,openimg5,openimg6,openimg01,openimg02,openimg03,openimg04,openimg05,openimg06;
    private Button openshopcreatebt;
    private CheckBox knowstate;
    private SharedPreferences sharedPreferences;
    private String token;
    private String userId;
    private String appId;
    private EditText inputaddress;
    private RelativeLayout getaddress;
    private String alatitude;
    private String alongitude;
    private String asnippet;
    private TextView textaddress;
    private EditText inexplain;
    private EditText inname;
    private String type="1";
    private String auditStatus;
    private String type1;
    private String id;
    private String avatar;
    private List<String> backshowlist = new ArrayList<>();
    private String isOpenedStore;
    private String name;
    private String explain;
    private AlertDialog dialog;
    private JSONArray showImages;
    private String isInsurance;
    private TextView title;
    private BaseIntents baseIntents;
    private EditText inhouseno;
    private String houseno;
    private JSONArray qualImages;
    private TextView baox;
    private int toastType = 0;
    private ImageView backmain;

    @Override
    protected void onRestart() {
        super.onRestart();
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        isInsurance = sharedPreferences.getString("isInsurance", "");
        if (isInsurance.equals("true")){
            knowstate.setChecked(true);
        }else {
            knowstate.setChecked(false);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fwopen_shop);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        token = sharedPreferences.getString("token", "");
        userId = sharedPreferences.getString("userId", "");
        appId = sharedPreferences.getString("appid", "");
        isInsurance = sharedPreferences.getString("isInsurance", "");
        Intent intent = getIntent();
        isOpenedStore = intent.getStringExtra("isOpenedStore");

        initOpenShopView();
        baseIntents = new BaseIntents();
        if (isInsurance!=null){

        }else {
            if (isInsurance.equals("true")){
                knowstate.setChecked(true);
            }else {
                knowstate.setChecked(false);
            }
        }

        possionshop.setSelected(true);
        teampic.setVisibility(View.GONE);
        if (isOpenedStore!=null){
            if (isOpenedStore.equals("false")) {
                inname.setFocusable(true);
                title.setText("店铺创建");
                openshopcreatebt.setText("创建店铺");
            } else {
                inname.setFocusable(false);
                title.setText("店铺编辑");
                openshopcreatebt.setText("店铺编辑");
                openshopcreatebt.setText("确认修改");
                AlertDialog.Builder builder = new AlertDialog.Builder(FWOpenShopActivity.this, R.style.NoBackGroundDialog);
                View view = LayoutInflater.from(this).inflate(R.layout.progress_dialog, null);
                builder.setView(view);
                dialog = builder.create();
                dialog.show();
                getShopMessage();
            }
        }else {}
    }
    // 编辑店铺
    private void EditShop() {
        getInPutData();

        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.SHOPEDITSTORE);
        params.addHeader("Content-Type","multipart/form-data");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        params.setMultipart(true);
        params.addBodyParameter("type",type1);
        params.addBodyParameter("name", name);
        params.addBodyParameter("intro", explain);
        params.addBodyParameter("shopId",id);
        params.addBodyParameter("shopHouseNo",houseno);
        if (asnippet==null){
            params.addBodyParameter("address", String.valueOf(textaddress.getText()));
        }else {
            params.addBodyParameter("address",asnippet);
        }
        if (alatitude==null){
        }else {
            params.addBodyParameter("latitude",alatitude);
        }
        if (alongitude==null){
        }else {
            params.addBodyParameter("longitude",alongitude);
        }

        if (qualificationimglist!=null){
            for (int i = 0; i < qualificationimglist.size(); i++) {
                params.addBodyParameter("qualification",new File(qualificationimglist.get(i)));
            }

        }else {
        }
        if (avatarimglist.size()!=0){
            for (int i = 0; i < avatarimglist.size(); i++) {
                params.addBodyParameter("avatar",new File(avatarimglist.get(i)));
            }

        }else {// 回传img
            params.addBodyParameter("avatar",avatar);
        }
        if (showimglist.size()!=0){
            switch (showimglist.size()){
                case 1:
                    params.addBodyParameter("show",new File(showimglist.get(0)));
                    if (backshowlist.size()>=5){
                        for (int i = 0; i < 5; i++) {
                            params.addBodyParameter("show",backshowlist.get(i));
                        }
                    }else {
                        for (int i = 0; i < backshowlist.size(); i++) {
                            params.addBodyParameter("show",backshowlist.get(i));
                        }
                    }
                    break;
                case 2:
                    params.addBodyParameter("show",new File(showimglist.get(0)));
                    params.addBodyParameter("show",new File(showimglist.get(1)));
                    if (backshowlist.size()>=4){
                        for (int i = 0; i < 4; i++) {
                            params.addBodyParameter("show",backshowlist.get(i));
                        }
                    }else {
                        for (int i = 0; i < backshowlist.size(); i++) {
                            params.addBodyParameter("show",backshowlist.get(i));
                        }
                    }
                    break;
                case 3:
                    params.addBodyParameter("show",new File(showimglist.get(0)));
                    params.addBodyParameter("show",new File(showimglist.get(1)));
                    params.addBodyParameter("show",new File(showimglist.get(2)));
                    if (backshowlist.size()>=3){
                        for (int i = 0; i < 3; i++) {
                            params.addBodyParameter("show",backshowlist.get(i));
                        }
                    }else {
                        for (int i = 0; i < backshowlist.size(); i++) {
                            params.addBodyParameter("show",backshowlist.get(i));
                        }
                    }
                    break;
                case 4:
                    params.addBodyParameter("show",new File(showimglist.get(0)));
                    params.addBodyParameter("show",new File(showimglist.get(1)));
                    params.addBodyParameter("show",new File(showimglist.get(2)));
                    params.addBodyParameter("show",new File(showimglist.get(3)));
                    if (backshowlist.size()>=2){
                        for (int i = 0; i < 2; i++) {
                            params.addBodyParameter("show",backshowlist.get(i));
                        }
                    }else {
                        for (int i = 0; i < backshowlist.size(); i++) {
                            params.addBodyParameter("show",backshowlist.get(i));
                        }
                    }
                    break;
                case 5:
                    params.addBodyParameter("show",new File(showimglist.get(0)));
                    params.addBodyParameter("show",new File(showimglist.get(1)));
                    params.addBodyParameter("show",new File(showimglist.get(2)));
                    params.addBodyParameter("show",new File(showimglist.get(3)));
                    params.addBodyParameter("show",new File(showimglist.get(4)));
                    if (backshowlist.size()>=1){
                        for (int i = 0; i < 1; i++) {
                            params.addBodyParameter("show",backshowlist.get(i));
                        }
                    }else {
                        for (int i = 0; i < backshowlist.size(); i++) {
                            params.addBodyParameter("show",backshowlist.get(i));
                        }
                    }
                    break;
                case 6:
                    params.addBodyParameter("show",new File(showimglist.get(0)));
                    params.addBodyParameter("show",new File(showimglist.get(1)));
                    params.addBodyParameter("show",new File(showimglist.get(2)));
                    params.addBodyParameter("show",new File(showimglist.get(3)));
                    params.addBodyParameter("show",new File(showimglist.get(4)));
                    params.addBodyParameter("show",new File(showimglist.get(5)));
                    break;
            }

        }else {// 回传
            for (int i = 0; i < backshowlist.size(); i++) {
                params.addBodyParameter("show",backshowlist.get(i));
           }

        }
    x.http().post(params, new Callback.CommonCallback<String>() {
        @Override
        public void onSuccess(String result) {
            Log.d("tag", "onSuccess: 编辑店铺--"+result);
            JSONObject obj = JSON.parseObject(result);
            String status = obj.getString("status");
            if (status.equals("1")){
                if (toastType==0){
                    Toast.makeText(FWOpenShopActivity.this, "店铺修改成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(FWOpenShopActivity.this, "店铺修改成功，请等待审核", Toast.LENGTH_SHORT).show();

                }

            }else {

            }
            dialog.dismiss();
        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {
            Log.d("tag", "onError: 编辑店铺---bianji -- "+ex);
            ErrType.errType(FWOpenShopActivity.this,ex);
        }

        @Override
        public void onCancelled(CancelledException cex) {

        }

        @Override
        public void onFinished() {
            isOpenedStore ="true";
            Intent intent = new Intent(FWOpenShopActivity.this,SouSuoAddNumActivity.class);
            intent.putExtra("isstate",isOpenedStore);
            startActivity(intent);
            finish();
        }
    });

    }
// 获取输入内容
    private void getInPutData() {
        name = String.valueOf(inname.getText());// 店铺名称
        explain = String.valueOf(inexplain.getText()); // 店铺简介
        houseno = String.valueOf(inhouseno.getText());// 门牌号

    }

    // 获取店铺信息
    private void getShopMessage() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETSTOREMESSAGE);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d("tag", "onSuccess: 店铺--"+result);
                JSONObject object = JSON.parseObject(result);
                String data = object.getString("data");
                JSONObject object1 = JSON.parseObject(data);
                String address1 = object1.getString("address");
                auditStatus = object1.getString("auditStatus");
                avatar = object1.getString("avatar");
                id = object1.getString("id");
                String intro = object1.getString("intro");
                String name1 = object1.getString("name");
                String shopHouseNo = object1.getString("shopHouseNo");
                qualImages = object1.getJSONArray("qualImages");
                type1 = object1.getString("type");
                SharedPreferences.Editor edit = sharedPreferences.edit();
                edit.putString("shopid", id);
                edit.putString("type", type1);
                edit.commit();
                if (type1.equals("1")){
                    possionshop.setSelected(true);
                    teamshop.setVisibility(View.GONE);
                    teampic.setVisibility(View.GONE);
                }else {
                    if (auditStatus.equals("-1")){
                        teampic.setVisibility(View.VISIBLE);
                    }else {
                        teampic.setVisibility(View.VISIBLE);
                    }
                    teamshop.setSelected(true);

                    possionshop.setVisibility(View.GONE);
                }

                inhouseno.setText(shopHouseNo);
                inname.setText(name1);
                textaddress.setText(address1);
                inexplain.setText(intro);
                x.image().bind(openimg,InternetAddressUtis.IMG_URL+ avatar);

                showImages = object1.getJSONArray("showImages");
                for (int i = 0; i < showImages.size(); i++) {
                    backshowlist.add(String.valueOf(showImages.get(i)));
                }
                int size = showImages.size();
                if (qualImages!=null){
                    switch (qualImages.size()){
                        case 1:
                            x.image().bind(openimg01,InternetAddressUtis.IMG_URL+qualImages.get(0));
                            break;
                        case 2:
                            x.image().bind(openimg01,InternetAddressUtis.IMG_URL+qualImages.get(0));
                            x.image().bind(openimg02,InternetAddressUtis.IMG_URL+qualImages.get(1));

                            break;
                        case 3:
                            x.image().bind(openimg01,InternetAddressUtis.IMG_URL+qualImages.get(0));
                            x.image().bind(openimg02,InternetAddressUtis.IMG_URL+qualImages.get(1));
                            x.image().bind(openimg03,InternetAddressUtis.IMG_URL+qualImages.get(2));

                            break;
                        case 4:
                            x.image().bind(openimg01,InternetAddressUtis.IMG_URL+qualImages.get(0));
                            x.image().bind(openimg02,InternetAddressUtis.IMG_URL+qualImages.get(1));
                            x.image().bind(openimg03,InternetAddressUtis.IMG_URL+qualImages.get(2));
                            x.image().bind(openimg04,InternetAddressUtis.IMG_URL+qualImages.get(3));
                            break;
                    }
                }
               switch (size){
                   case 1:
                       x.image().bind(openimg1,InternetAddressUtis.IMG_URL+ showImages.get(0));
                       break;
                   case 2:
                       x.image().bind(openimg1,InternetAddressUtis.IMG_URL+ showImages.get(0));
                       x.image().bind(openimg2,InternetAddressUtis.IMG_URL+ showImages.get(1));
                       break;
                   case 3:
                       x.image().bind(openimg1,InternetAddressUtis.IMG_URL+ showImages.get(0));
                       x.image().bind(openimg2,InternetAddressUtis.IMG_URL+ showImages.get(1));
                       x.image().bind(openimg3,InternetAddressUtis.IMG_URL+ showImages.get(2));
                       break;
                   case 4:
                       x.image().bind(openimg1,InternetAddressUtis.IMG_URL+ showImages.get(0));
                       x.image().bind(openimg2,InternetAddressUtis.IMG_URL+ showImages.get(1));
                       x.image().bind(openimg3,InternetAddressUtis.IMG_URL+ showImages.get(2));
                       x.image().bind(openimg4,InternetAddressUtis.IMG_URL+ showImages.get(3));
                       break;
                   case 5:
                       x.image().bind(openimg1,InternetAddressUtis.IMG_URL+ showImages.get(0));
                       x.image().bind(openimg2,InternetAddressUtis.IMG_URL+ showImages.get(1));
                       x.image().bind(openimg3,InternetAddressUtis.IMG_URL+ showImages.get(2));
                       x.image().bind(openimg4,InternetAddressUtis.IMG_URL+ showImages.get(3));
                       x.image().bind(openimg5,InternetAddressUtis.IMG_URL+ showImages.get(4));
                       break;
                   case 6:
                       x.image().bind(openimg1,InternetAddressUtis.IMG_URL+ showImages.get(0));
                       x.image().bind(openimg2,InternetAddressUtis.IMG_URL+ showImages.get(1));
                       x.image().bind(openimg3,InternetAddressUtis.IMG_URL+ showImages.get(2));
                       x.image().bind(openimg4,InternetAddressUtis.IMG_URL+ showImages.get(3));
                       x.image().bind(openimg5,InternetAddressUtis.IMG_URL+ showImages.get(4));
                       x.image().bind(openimg6,InternetAddressUtis.IMG_URL+ showImages.get(5));
                       break;
               }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(FWOpenShopActivity.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                dialog.dismiss();
            }
        });
    }
    // 创建店铺
    private void OnCreateShop() {
       getInPutData();
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.OPENSTORE);
        params.addHeader("Content-Type","multipart/form-data");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        params.setMultipart(true);
        params.addBodyParameter("type",type);
        params.addBodyParameter("name",name);
        params.addBodyParameter("intro",explain);
        params.addBodyParameter("shopHouseNo",houseno);
        params.addBodyParameter("address",asnippet);
        params.addBodyParameter("longitude",alongitude);
        params.addBodyParameter("latitude",alatitude);
        if (avatarimglist.size()!=0){
            for (int i = 0; i < avatarimglist.size(); i++) {
                params.addBodyParameter("avatar",new File(avatarimglist.get(i)));
            }
        }else {}
        if (showimglist.size()!=0){
            for (int j = 0; j < showimglist.size(); j++) {
                params.addBodyParameter("show",new File(showimglist.get(j)));
            }
        }else {}
        if (qualificationimglist.size()!=0){
            for (int m = 0; m < qualificationimglist.size(); m++) {
                params.addBodyParameter("qualification",new File(qualificationimglist.get(m)));
            }
        }else {}
        Log.d("tag", "OnCreateShop: 开店--"+token+"//"+appId+"//"+userId+"//"+type+"//"+name+"//"+explain+"//"+houseno+"//"+asnippet+"//"+alongitude+"//"+alatitude+"//"+avatarimglist.size()+"//"+showimglist.size()+"//"+qualificationimglist.size());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d("tag", "onSuccess: 开店--"+result);
                Toast.makeText(FWOpenShopActivity.this, "开店成功 请耐心等待审核", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("tag", "onError: 开店--"+ex);
                ErrType.errType(FWOpenShopActivity.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                isOpenedStore ="true";
                Intent intent = new Intent(FWOpenShopActivity.this,SouSuoAddNumActivity.class);
                intent.putExtra("isstate",isOpenedStore);
                startActivity(intent);
                finish();
            }
        });
    }

    private void initOpenShopView() {
        backmain = (ImageView) findViewById(R.id.fwopenshop_backmain);
        baox = (TextView) findViewById(R.id.openshop_baomoneyx);
        inhouseno = (EditText) findViewById(R.id.openshop_inhouseno);
        possionshop = (Button) findViewById(R.id.fw_openshop_possion);
        teamshop = (Button) findViewById(R.id.fw_openshop_team);
        openshopback = (ImageView) findViewById(R.id.fw_openshop_back);
        teampic = (LinearLayout) findViewById(R.id.fw_openshop_teampic);
        openimg = (ImageView) findViewById(R.id.fwopen_img);
        openimg1 = (ImageView) findViewById(R.id.fwopen_img1);
        openimg2 = (ImageView) findViewById(R.id.fwopen_img2);
        openimg3 = (ImageView) findViewById(R.id.fwopen_img3);
        openimg4 = (ImageView) findViewById(R.id.fwopen_img4);
        openimg5 = (ImageView) findViewById(R.id.fwopen_img5);
        openimg6 = (ImageView) findViewById(R.id.fwopen_img6);
        openimg01 = (ImageView) findViewById(R.id.fwopen_img01);
        openimg02 = (ImageView) findViewById(R.id.fwopen_img02);
        openimg03 = (ImageView) findViewById(R.id.fwopen_img03);
        openimg04 = (ImageView) findViewById(R.id.fwopen_img04);
        openshopcreatebt = (Button) findViewById(R.id.openshop_createbt);
        knowstate = (CheckBox) findViewById(R.id.openshop_stateknow);
        getaddress = (RelativeLayout) findViewById(R.id.openshop_getaddress);
        textaddress = (TextView) findViewById(R.id.openshop_inputaddress);
        inexplain = (EditText) findViewById(R.id.openshop_inexplain);
        inname = (EditText) findViewById(R.id.openshop_inname);
        title = (TextView) findViewById(R.id.openshop_top_title);
        backmain.setOnClickListener(this);
        knowstate.setOnClickListener(this);
        openshopcreatebt.setOnClickListener(this);
        possionshop.setOnClickListener(this);
        teamshop.setOnClickListener(this);
        openshopback.setOnClickListener(this);
        openimg.setOnClickListener(this);
        openimg1.setOnClickListener(this);
        openimg2.setOnClickListener(this);
        openimg3.setOnClickListener(this);
        openimg4.setOnClickListener(this);
        openimg5.setOnClickListener(this);
        openimg6.setOnClickListener(this);
        openimg01.setOnClickListener(this);
        openimg02.setOnClickListener(this);
        openimg03.setOnClickListener(this);
        openimg04.setOnClickListener(this);
        getaddress.setOnClickListener(this);
        baox.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fwopenshop_backmain:

                startActivity(new Intent(FWOpenShopActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.openshop_baomoneyx:
                Intent i = new Intent(FWOpenShopActivity.this,WEBXActivity.class);
                i.putExtra("from","open");
                i.putExtra("url",InternetAddressUtis.XYIVIBAOMONEY);
                startActivity(i);
                break;
            case R.id.openshop_getaddress:
                Intent intent = new Intent(FWOpenShopActivity.this,InPOISearchActivity.class);
                startActivityForResult(intent,FROM);
                break;
            case R.id.openshop_stateknow:
               if (!isInsurance.equals("true")){
                   startActivity(new Intent(FWOpenShopActivity.this,MineMoney.class));
               }else {

               }
                break;
            case R.id.openshop_createbt:
                AlertDialog.Builder builder = new AlertDialog.Builder(FWOpenShopActivity.this,R.style.NoBackGroundDialog);
                View view = LayoutInflater.from(this).inflate(R.layout.progress_dialog, null);
                ProgressBar diapro = (ProgressBar) view.findViewById(R.id.pro_dia);
                builder.setView(view);
                dialog = builder.create();
                dialog.show();
                if (isOpenedStore.equals("false")){ // 未开店状态  开店
                    if (type.equals("1")){
                        OnCreateShop();
                    }else {
                        if (qualificationimglist.size()==4&showimglist.size()>=1){
                            OnCreateShop();
                        }else {
                            Toast.makeText(this, "请补全信息", Toast.LENGTH_SHORT).show();
                        }

                    }

                }else {  // 开店状态   编辑店铺
                    if (type1.equals("1")){// 个人 不需要资质
                        if (auditStatus.equals("-1")){
                            EditShop();
                        }else if (auditStatus.equals("1")){
                            EditShop();
                        }else {
                            EditShop();
                            Toast.makeText(this, "您的店铺资料正在审核中，请耐心等待", Toast.LENGTH_LONG).show();
                        }
                    }else { // 企业 需要资质
                        if (auditStatus.equals("-1")){
                            EditShop();
                        }else if (auditStatus.equals("1")){
                            EditShop();
                        }else {
                            EditShop();
                            Toast.makeText(this, "您的店铺资料正在审核中，请耐心等待", Toast.LENGTH_LONG).show();
                        }
                    }
                }

                break;
            case R.id.fw_openshop_back:
                finish();
                break;
            case R.id.fw_openshop_possion:
                type = "1";
                possionshop.setSelected(true);
                teamshop.setSelected(false);
                teampic.setVisibility(View.GONE);
                break;
            case R.id.fw_openshop_team:
                type = "2";
                teamshop.setSelected(true);
                possionshop.setSelected(false);
                teampic.setVisibility(View.VISIBLE);
                break;
            case R.id.fwopen_img:
                toastType = 1;
                imgtype=1;
                currentIndex=0;
                doPickPhotoAction();
                break;
            case R.id.fwopen_img1:
                toastType = 1;
                imgtype=2;
                currentIndex=1;

                if (showImages!=null&&showImages.size()>=1){
                    removePic(String.valueOf(showImages.get(0)));
                }else {}

                doPickPhotoAction();
                break;
            case R.id.fwopen_img2:
                toastType = 1;
                imgtype=2;
                currentIndex=2;
                if (showImages!=null&&showImages.size()>=2){
                    removePic(String.valueOf(showImages.get(1)));
                }else {}

                doPickPhotoAction();
                break;
            case R.id.fwopen_img3:
                toastType = 1;
                imgtype=2;
                currentIndex=3;
                if (showImages!=null&&showImages.size()>=3){
                    removePic(String.valueOf(showImages.get(2)));
                }else {}

                doPickPhotoAction();
                break;
            case R.id.fwopen_img4:
                toastType = 1;
                imgtype=2;
                currentIndex=4;
                if (showImages!=null&&showImages.size()>=4){
                    removePic(String.valueOf(showImages.get(3)));
                }else {}

                doPickPhotoAction();
                break;
            case R.id.fwopen_img5:
                toastType = 1;
                imgtype=2;
                currentIndex=5;
                if (showImages!=null&&showImages.size()>=5){
                    removePic(String.valueOf(showImages.get(1)));
                }else {}
                doPickPhotoAction();
                break;
            case R.id.fwopen_img6:
                toastType = 1;
                imgtype=2;
                currentIndex=6;
                if (showImages!=null&&showImages.size()>=6){
                    removePic(String.valueOf(showImages.get(5)));
                }else {}

                doPickPhotoAction();
                break;
            case R.id.fwopen_img01:
                toastType = 1;
                imgtype=3;
                currentIndex=7;
                doPickPhotoAction();
                break;
            case R.id.fwopen_img02:
                toastType = 1;
                imgtype=3;
                currentIndex=8;
                doPickPhotoAction();
                break;
            case R.id.fwopen_img03:
                toastType = 1;
                imgtype=3;
                currentIndex=9;
                doPickPhotoAction();
                break;
            case R.id.fwopen_img04:
                toastType = 1;
                imgtype=3;
                currentIndex=10;
                doPickPhotoAction();
                break;
        }
    }
        // 点击删除图片
    private void removePic(String s) {
        for (int i = 0; i < backshowlist.size(); i++) {
            if (s.equals(backshowlist.get(i))){
                backshowlist.remove(i);
            }
        }

    }


    private void doPickPhotoAction() {
        final Context context = this;
        AlertDialog.Builder b1 = new AlertDialog.Builder(context);
        View view = LayoutInflater.from(context).inflate(R.layout.picture_dialog, null);
        b1.setView(view);

        TextView camere = (TextView) view.findViewById(R.id.picdia_camere);
        TextView pic = (TextView) view.findViewById(R.id.picdia_pic);
        TextView cancel1 = (TextView) view.findViewById(R.id.picdia_cancel);

        final AlertDialog dialog1 = b1.create();
        Window window = dialog1.getWindow();
        camere.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String status= Environment.getExternalStorageState();
                if(status.equals(Environment.MEDIA_MOUNTED)){//判断是否有SD卡
                    // index=0;
                    baseIntents.doTakePhoto();// 用户点击了从照相机获取
                    dialog1.dismiss();
                }
                else{
                    Toast.makeText(context, "没有SD卡", Toast.LENGTH_SHORT).show();
                }
            }
        });
        pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                baseIntents.doPickPhotoFromGallery();// 从相册中去获取
                dialog1.dismiss();
            }
        });
        cancel1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog1.dismiss();
            }
        });
        dialog1.show();
        window.setGravity(Gravity.BOTTOM);
    }
    // 因为调用了Camera和Gally所以要判断他们各自的返回情况,他们启动时是这样的startActivityForResult
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode==FROM){

            if (data==null){

            }else {

                alatitude = data.getStringExtra("latitude");
                alongitude = data.getStringExtra("longitude");
                asnippet = data.getStringExtra("snippet");
                textaddress.setText(asnippet);

            }

        }else {}
        if (resultCode != RESULT_OK)
            return;
        switch (requestCode) {
            case PHOTO_PICKED_WITH_DATA: {// 调用Gallery返回的
                Bitmap photo = data.getParcelableExtra("data");

                String s = CropPictures.saveBitmapFile(photo, baseIntents.getPhotoFileName());

                if (imgtype==1){
                    avatarimglist.add(s);
                }else if (imgtype==2){
                    showimglist.add(s);
                }else {
                    qualificationimglist.add(s);
                }
                if (currentIndex==0){
                    openimg.setImageBitmap(photo);
                }else if (currentIndex==1){
                    openimg1.setImageBitmap(photo);
                }else if (currentIndex==2){
                    openimg2.setImageBitmap(photo);
                }else if (currentIndex==3){
                    openimg3.setImageBitmap(photo);
                }else if (currentIndex==4){
                    openimg4.setImageBitmap(photo);
                }else if (currentIndex==5){
                    openimg5.setImageBitmap(photo);
                }else  if (currentIndex==6){
                    openimg6.setImageBitmap(photo);
                }else if (currentIndex==7){
                    openimg01.setImageBitmap(photo);
                }else if (currentIndex==8){
                    openimg02.setImageBitmap(photo);
                }else if (currentIndex==9){
                    openimg03.setImageBitmap(photo);
                }else if (currentIndex==10){
                    openimg04.setImageBitmap(photo);
                }else if (currentIndex==11){
                    openimg05.setImageBitmap(photo);
                }else  if (currentIndex==12){
                    openimg06.setImageBitmap(photo);
                }
                break;
            }
            case CAMERA_WITH_DATA: {// 照相机程序返回的,再次调用图片剪辑程序去修剪图片
                baseIntents.doCropPhoto(baseIntents.mCurrentPhotoFile);
                break;
            }
            case PHOTO_TEXT:
                Uri data1 = data.getData();
                if (!TextUtils.isEmpty(data1.getAuthority())){
                    ContentResolver resolver = getContentResolver();
                    try {
                        photo = MediaStore.Images.Media.getBitmap(resolver, data1);
                        String[] proj = {MediaStore.Images.Media.DATA};
                        Cursor cursor = managedQuery(data1, proj, null, null, null);
                        if (cursor==null){
                            Toast.makeText(this, "没有找到图片", Toast.LENGTH_SHORT).show();
                        }else {
                            cursor.moveToFirst();
                            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                            //图片路径
                            path1 = cursor.getString(column_index);

                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else {
                    path1 = data1.getPath();
                }
                File file = new File(path1);
                baseIntents.doCropPhoto(file);
                break;
        }

    }

}
